function modify_name(ev, ok) {
        if($("#nickcheckform").valid() === true) {
                ev.stopPropagation();
                ok = (typeof ok != 'undefined') ? ok : false;
                if (ok) return true;
                $.blockUI({ message: $('#question_modify_name'), css: { width: '400px',height:'100px',border:'10px solid #999999'} });
                return false;
        }
}
function modify_photo(ev, ok) {
        if($("#photochangeform").valid() === true) {
                ev.stopPropagation();
                ok = (typeof ok != 'undefined') ? ok : false;
                if (ok) return true;
                $.blockUI({ message: $('#question_modify_photo'), css: { width: '400px',height:'100px',border:'10px solid #999999'} });
                return false;
        }
}
function modify_password(ev, ok) {
        if($("#passwdchangeform").valid() === true) {
                ev.stopPropagation();
                ok = (typeof ok != 'undefined') ? ok : false;
                if (ok) return true;
                $.blockUI({ message: $('#question_modify_password'), css: { width: '400px',height:'100px',border:'10px solid #999999'} });
                return false;
        }
}
function modify_renren(ev, ok) {
	ev.stopPropagation();
	ok = (typeof ok != 'undefined') ? ok : false;
	if (ok) return true;
	$.blockUI({ message: $('#question_delete_renren'), css: { width: '400px',height:'100px',border:'10px solid #999999'} });
	return false;
}
function modify_taobao(ev, ok) {
	ev.stopPropagation();
	ok = (typeof ok != 'undefined') ? ok : false;
	if (ok) return true;
	$.blockUI({ message: $('#question_delete_taobao'), css: { width: '400px',height:'100px',border:'10px solid #999999'} });
	return false;
}
$(document).ready(function() {

	var validator1 = $("#nickcheckform").validate({
		rules: {
			username: {
				required: true,
				maxlength: 12,
				remote: "/users/check_username"
			}
		},
		messages: {
			username: {
				required: "取个好听的呢称吧亲!",
				minlength: jQuery.format("请输入最少 {0} 个字符"),
				maxlength: jQuery.format("请输入最多 {0} 个字符"),
				remote: jQuery.format("{0} 已经被人注册了")
			}
		},
		// the errorPlacement has to take the table layout into account
		errorPlacement: function(error, element) {
			if ( element.is(":radio") )
				error.appendTo( element.parent().next().next() );
			else if ( element.is(":checkbox") )
				error.appendTo ( element.next() );
			else
				error.appendTo( element.parent().next() );
		},
		// set this class to error-labels to indicate valid fields
		success: function(label) {
			// set &nbsp; as text for IE
			label.html("&nbsp;").addClass("checked");
		}
	});

	var validator2 = $("#photochangeform").validate({
		rules: {
			image: {
				required: true
			}
		},
		messages: {
			image: {
				required: "请先选择一张照片"
			}
		},
		// the errorPlacement has to take the table layout into account
		errorPlacement: function(error, element) {
			if ( element.is(":radio") )
				error.appendTo( element.parent().next().next() );
			else if ( element.is(":checkbox") )
				error.appendTo ( element.next() );
			else
				error.appendTo( element.parent().next() );
		},
		// set this class to error-labels to indicate valid fields
		success: function(label) {
			// set &nbsp; as text for IE
			label.html("&nbsp;").addClass("checked");
		}
	});

	var validator3 = $("#passwdchangeform").validate({
		rules: {
			old_password: {
                                required: true,
                                minlength: 6,
				remote: "/users/check_passwd"
                        },
			new_password: {
                                required: true,
                                minlength: 6
                        },
                        password_confirm: {
                                required: true,
                                minlength: 6,
                                equalTo: "#new_password"
                        }
		},
		messages: {
			old_password: {
				required: "请输入密码",
				rangelength: jQuery.format("请输入最少 {0} 个字符"),
				remote: jQuery.format("密码不对")

                        },
			new_password: {
				required: "请输入密码",
				rangelength: jQuery.format("请输入最少 {0} 个字符")
                        },
                        password_confirm: {
				required: "请再次输入密码",
				minlength: jQuery.format("请输入最少 {0} 个字符"),
				equalTo: "请确认您输入的是相同的密码"

                        }
		},
		// the errorPlacement has to take the table layout into account
		errorPlacement: function(error, element) {
			if ( element.is(":radio") )
				error.appendTo( element.parent().next().next() );
			else if ( element.is(":checkbox") )
				error.appendTo ( element.next() );
			else
				error.appendTo( element.parent().next() );
		},
		// set this class to error-labels to indicate valid fields
		success: function(label) {
			// set &nbsp; as text for IE
			label.html("&nbsp;").addClass("checked");
		}
	});


	var items_on_page = 10;
	var pages_step = 5;

//	get_data_tuan_zhuanyis(1, 10);
//	$.ajax({
//		type: "POST",
//		url: "/users/zhuanyis_total",
//		data: {
//		user_id: $("#user_id").val(),
//		type: "get"},
//		success: function(total)
//		{
//			$("#paging_zhuanyis").jpaging({
//				all_items_num: total,
//				callback: get_data_tuan_zhuanyis,
//				items_on_page: items_on_page,
//				pages_step: pages_step
//			});
//		}
//	});

	get_data_tuanzhangs(1, 10);
	$.ajax({
		type: "POST",
		url: "/users/tuanzhang_total",
		data: {
		user_id: $("#user_id").val(),
		type: "get"},
		success: function(total)
		{
			$("#paging_tuanzhangs").jpaging({
				all_items_num: total,
				callback: get_data_tuanzhangs,
				items_on_page: items_on_page,
				pages_step: pages_step
			});
		}
	});

	get_data_tuans(1, 10);
	$.ajax({
		type: "POST",
		url: "/users/tuans_total",
		data: {
		user_id: $("#user_id").val(),
		type: "get"},
		success: function(total)
		{
			$("#paging_tuans").jpaging({
				all_items_num: total,
				callback: get_data_tuans,
				items_on_page: items_on_page,
				pages_step: pages_step
			});
		}
	});

	get_data_buys(1, 10);
	$.ajax({
		type: "POST",
		url: "/users/buys_total",
		data: {
		user_id: $("#user_id").val(),
		type: "get"},
		success: function(total)
		{
			$("#paging_buys").jpaging({
				all_items_num: total,
				callback: get_data_buys,
				items_on_page: items_on_page,
				pages_step: pages_step
			});
		}
	});

	get_data_baobei(1, 10);
	$.ajax({
		type: "POST",
		url: "/users/baobeis_total",
		data: {
		user_id: $("#user_id").val(),
		type: "get"},
		success: function(total)
		{
			$("#paging_baobei").jpaging({
				all_items_num: total,
				callback: get_data_baobei,
				items_on_page: items_on_page,
				pages_step: pages_step
			});
		}
	});

	get_data_news(1, 10);
	$.ajax({
		type: "POST",
		url: "/users/news_total",
		data: {
		user_id: $("#user_id").val(),
		type: "get"},
		success: function(total)
		{
			$("#paging_news").jpaging({
				all_items_num: total,
				callback: get_data_news,
				items_on_page: items_on_page,
				pages_step: pages_step
			});
		}
	});

	get_data_comments(1, 10);
	$.ajax({
		type: "POST",
		url: "/users/comments_total",
		data: {
		user_id: $("#user_id").val(),
		type: "get"},
		success: function(total)
		{
			$("#paging_comments").jpaging({
				all_items_num: total,
				callback: get_data_comments,
				items_on_page: items_on_page,
				pages_step: pages_step
			});
		}
	});

//	function get_data_tuan_zhuanyis(start_index, end_index){
//		start_index = start_index - 1;
//		end_index = end_index - 1;
//		$.ajax({
//			type: "POST",
//			url: "/users/zhuanyi_info",
//			data: {start_index: start_index,
//			end_index: end_index,
//			user_id: $("#user_id").val(),
//			type: "get"},
//			success: function(html)
//			{
//				$("#demo_tbl_zhuanyis").html(html);
//				$('html, body').animate({ scrollTop: 0 }, 'slow');
//			}
//		});
//	}

	function get_data_tuanzhangs(start_index, end_index){
		start_index = start_index - 1;
		end_index = end_index - 1;
		$.ajax({
			type: "POST",
			url: "/users/tuanzhang_info1",
			data: {start_index: start_index,
			end_index: end_index,
			user_id: $("#user_id").val(),
			type: "get"},
			success: function(html)
			{
				$("#demo_tbl_tuanzhangs").html(html);
				$('html, body').animate({ scrollTop: 0 }, 'slow');
			}
		});
	}


	function get_data_tuans(start_index, end_index){
		start_index = start_index - 1;
		end_index = end_index - 1;
		$.ajax({
			type: "POST",
			url: "/users/tuans_paging",
			data: {start_index: start_index,
			end_index: end_index,
			user_id: $("#user_id").val(),
			type: "get"},
			success: function(html)
			{
				$("#demo_tbl_tuans").html(html);
				$('html, body').animate({ scrollTop: 0 }, 'slow');
				$.prettyDate.now = function() {
					return new Date();
				}
				$(".prettyDate").prettyDate();
			}
		});
	}

	function get_data_news(start_index, end_index){
		start_index = start_index - 1;
		end_index = end_index - 1;
		$.ajax({
			type: "POST",
			url: "/users/news_paging",
			data: {start_index: start_index,
			end_index: end_index,
			user_id: $("#user_id").val(),
			type: "get"},
			success: function(html)
			{
				$("#demo_tbl_news").html(html);
				$('html, body').animate({ scrollTop: 0 }, 'slow');
				 $.prettyDate.now = function() {
                                        return new Date();
                                }
                                $(".prettyDate").prettyDate();
			}
		});
	}

	function get_data_buys(start_index, end_index){
		start_index = start_index - 1;
		end_index = end_index - 1;
		$.ajax({
			type: "POST",
			url: "/users/buys_paging",
			data: {start_index: start_index,
			end_index: end_index,
			user_id: $("#user_id").val(),
			type: "get"},
			success: function(html)
			{
				$("#demo_tbl_buys").html(html);
				$('html, body').animate({ scrollTop: 0 }, 'slow');
			}
		});
	}

	function get_data_baobei(start_index, end_index){
		start_index = start_index - 1;
		end_index = end_index - 1;
		$.ajax({
			type: "POST",
			url: "/users/baobeis_paging",
			data: {start_index: start_index,
			end_index: end_index,
			user_id: $("#user_id").val(),
			type: "get"},
			success: function(html)
			{
				$("#demo_tbl_baobei").html(html);
				$('html, body').animate({ scrollTop: 0 }, 'slow');
			}
		});
	}

	function get_data_comments(start_index, end_index){
		start_index = start_index - 1;
		end_index = end_index - 1;
		$.ajax({
			type: "POST",
			url: "/users/comments_paging",
			data: {start_index: start_index,
			end_index: end_index,
			user_id: $("#user_id").val(),
			type: "get"},
			success: function(html)
			{
				$("#demo_tbl_comments").html(html);
				$.prettyDate.now = function() {
					return new Date();
				}
				$(".prettyDate").prettyDate();
				$('html, body').animate({ scrollTop: 0 }, 'slow');
			}
		});
	}

	$("#post_comment_button").click(function(){
		var comment = $("#user_comment_text").val();
		if(comment.length > 0) {
			$("#user_comment_text").attr("value", '');
			comment = comment.replace('\n', '<br />');
			$.ajax({
				type: "POST",
				url: "/users/comment",
				data: {
				comm_id: $("#comm_id").val(),
				user_id: $("#user_id").val(),
				comment: comment,
				type: "get"},
				success: function(res)
				{
					get_data_comments(1, 10);
					$.ajax({
						type: "POST",
						url: "/users/comments_total",
						data: {
						user_id: $("#user_id").val(),
						type: "get"},
						success: function(total)
						{
							$("#paging_comments").jpaging({
								all_items_num: total,
								callback: get_data_comments,
								items_on_page: items_on_page,
								pages_step: pages_step
							});
						}
					});
				}
			});
		}

	});

	$("#join_friends_button").click(function(){
                $.ajax({
                        type: "POST",
                        url: "/users/join_friends",
                        data: {friend_id: $("#user_id").val(),
			friend_name: $("#user_name").val(),
                        type: "get"},
                        success: function(html)
                        {
                                $("#join_friends_div").html(html);
                        }
                });
        }); 

	$('#nickcheckform').bind('submit', modify_name);
        $('#yes_name').click(function() {
                $('#nickcheckform').trigger('submit', [true]);
                $.blockUI({ message: "<div style=\"margin-left:50px;flaot:left;margin-top:15px;font-size:14px;color:#999999;\">处理中...请不要关闭该页面！</div>" , css:{height:'50px',width:'400px',border:'10px solid #999999'}});
        });

        $('#no_name').click(function() {
                $.unblockUI();
                return false;
                });
	$('#photochangeform').bind('submit', modify_photo);
        $('#yes_photo').click(function() {
                $('#photochangeform').trigger('submit', [true]);
                $.blockUI({ message: "<div style=\"margin-left:50px;flaot:left;margin-top:15px;font-size:14px;color:#999999;\">处理中...请不要关闭该页面！</div>" , css:{height:'50px',width:'400px',border:'10px solid #999999'}});
        });

        $('#no_photo').click(function() {
                $.unblockUI();
                return false;
                });
	$('#passwdchangeform').bind('submit', modify_password);
        $('#yes_password').click(function() {
                $('#passwdchangeform').trigger('submit', [true]);
                $.blockUI({ message: "<div style=\"margin-left:50px;flaot:left;margin-top:15px;font-size:14px;color:#999999;\">处理中...请不要关闭该页面！</div>" , css:{height:'50px',width:'400px',border:'10px solid #999999'}});
        });

        $('#no_password').click(function() {
                $.unblockUI();
                return false;
                });
	$('#renrenchangeform').bind('submit', modify_renren);
        $('#yes_delete_renren').click(function() {
                $('#renrenchangeform').trigger('submit', [true]);
                $.blockUI({ message: "<div style=\"margin-left:50px;flaot:left;margin-top:15px;font-size:14px;color:#999999;\">处理中...请不要关闭该页面！</div>" , css:{height:'50px',width:'400px',border:'10px solid #999999'}});
        });

        $('#no_delete_renren').click(function() {
                $.unblockUI();
                return false;
	});
	$('#taobaochangeform').bind('submit', modify_taobao);
        $('#yes_delete_taobao').click(function() {
                $('#taobaochangeform').trigger('submit', [true]);
                $.blockUI({ message: "<div style=\"margin-left:50px;flaot:left;margin-top:15px;font-size:14px;color:#999999;\">处理中...请不要关闭该页面！</div>" , css:{height:'50px',width:'400px',border:'10px solid #999999'}});
        });

        $('#no_delete_taobao').click(function() {
                $.unblockUI();
                return false;
	});

	$('#yes').click(function() {
                $.ajax({
                        type: "POST",
                        url: "/users/delete_buy_intention",
                        data: {
                        user_id: $("#user_id").val(),
                        store_id: $("#storeId").val(),
                        type: "get"},
                        success: function(ret)
                        {
                                get_data_buys(1, 10);
                                $.ajax({
                                        type: "POST",
                                        url: "/users/buys_total",
                                        data: {
                                        user_id: $("#user_id").val(),
                                        type: "get"},
                                        success: function(total)
                                        {
                                                $("#paging_buys").jpaging({
                                                        all_items_num: total,
                                                        callback: get_data_buys,
                                                        items_on_page: items_on_page,
                                                        pages_step: pages_step
                                                });
                                                $.unblockUI();
                                        }
                                });
                        }
                });
                $.blockUI({ message: "<div style=\"margin-left:50px;flaot:left;margin-top:15px;color:#999999;\">处理中...请不要关闭该页面！</h1>" , css:{height:'50px',width:'400px',border:'10px solid #999999'}});
        });

        $('#no').click(function() {
                $.unblockUI();
                return false;
        });
	
	$('#yes_delete_store').click(function() {
                $.ajax({
                        type: "POST",
                        url: "/stores/xiajia",
                        data: {
                        store_id: $("#storeId").val(),
                        type: "get"},
                        success: function(ret)
                        {
                                get_data_baobei(1, 10);
                                $.ajax({
                                        type: "POST",
                                        url: "/users/baobeis_total",
                                        data: {
                                        user_id: $("#user_id").val(),
                                        type: "get"},
                                        success: function(total)
                                        {
                                                $("#paging_baobei").jpaging({
                                                        all_items_num: total,
                                                        callback: get_data_baobei,
                                                        items_on_page: items_on_page,
                                                        pages_step: pages_step
                                                });
                                                $.unblockUI();
                                        }
                                });
                        }
                });
                $.blockUI({ message: "<div style=\"margin-left:50px;flaot:left;margin-top:15px;color:#999999;\">处理中...请不要关闭该页面！</h1>" , css:{height:'50px',width:'400px',border:'10px solid #999999'}});
        });

        $('#no_delete_store').click(function() {
                $.unblockUI();
                return false;
        });

	$('#tabs').css("visibility", "visible");
});
